__author__ = 'zhenjiao.su'
# -*- coding: utf-8 -*-
from excelrw import readxls
from selenium.webdriver.support.wait import WebDriverWait
from Newwebdriver import Active
from Other import loading
from random import uniform
class detail(Active):
    def __init__(self,driver):
        Active.__init__(self,driver)
        self.driver=driver
        self.elread=readxls("D:/pywebdriver/Pythonwebdriver/element.xls")
        self.detailtip=(self.elread(1,9),self.elread(1,10))  #deatil的提示，用来判断是否进入detail
        self.detailave=(self.elread(2,9),self.elread(2,10))  #均价
        self.detailsum=(self.elread(3,9),self.elread(3,10))  #总价
        self.detailhotelnames=(self.elread(4,9),self.elread(4,10))
        self.detailgoflightnames=(self.elread(5,9),self.elread(5,10))
        self.detailendflightnames=(self.elread(6,9),self.elread(6,10))
        self.detailonetype=(self.elread(20,9),self.elread(20,10))  #detail页第一个房间类型名
        self.flightsearch=(self.elread(7,9),self.elread(7,10))
        self.hotelsearch=(self.elread(8,9),self.elread(8,10))
        self.pageover=(self.elread(9,9),self.elread(9,10))    #全部卖完的重搜
        self.detail_loading=(self.elread(10,9),self.elread(10,10))   #detail提交的loading状态
        self.detail_roomlist=(self.elread(11,9),self.elread(11,10))   #酒店房间列表
        self.detail_morerometype=(self.elread(12,9),self.elread(12,10))   #更多酒店房型
        self.detail_roomcheckstatu=(self.elread(13,9),self.elread(13,10))   #酒店房型已被选中状态
        self.detail_roomcheck=(self.elread(14,9),self.elread(14,10))   #酒店房型选中按钮
        self.detail_roomtypename=(self.elread(15,9),self.elread(15,10))   #酒店房型列表某房间名称
        self.detail_uptateflight=(self.elread(18,9),self.elread(18,10))
        self.detail_updatehotel=(self.elread(19,9),self.elread(19,10))
        self.detail_nobutton=(self.elread(16,9),self.elread(16,10))    #灰色按钮
        self.detail_yesbutton=(self.elread(17,9),self.elread(17,10))   #非灰色按钮
    def detailpageexit(self):  #判断是否在detail页
        try:
            WebDriverWait(self.find_element(self.detailtip),10).until(lambda a:a)
            return True
        except:
            return False
    def detailattribute(self):    #获取detail页相关参数
        detailaveprice=self.find_element(self.detailave).get_attribute("textContent")  #获得均价
        self.detailaveprice=(detailaveprice.encode('utf-8')).strip("¥") #均价去除金钱符号
        detailsumprice=self.find_element(self.detailsum).get_attribute("textContent")
        self.detailsumprice=(detailsumprice.encode('utf-8')).strip("¥")  #总价
        self.detailhotelname=self.find_element(self.detailhotelnames).text   #酒店名称
        self.detailgoflightname=self.find_element(self.detailgoflightnames).text #去程航空公司
        self.detailendflightname=self.find_element(self.detailendflightnames).text  #回程航空公司
        self.detailonetypename=self.find_element(self.detailonetype).get_attribute("title") #detail酒店列表第1个房间类型
    def overexit(self,pageexitnumber=None):    #自定义判断页面售罄类型
        if pageexitnumber==1:
            pageexit=self.flightsearch    #机票
        elif pageexitnumber==2:
            pageexit=self.hotelsearch  #酒店
        elif pageexitnumber==3:
            pageexit=self.pageover  #全部卖完
        else:
            print(u"参数不存在")
            raise SystemError
        try:
            self.find_element(pageexit)
            return True
        except:
            return False
    def detailloadexit(self,timeout=30):   #判断提交载入状态
        return loading(self.find_element(self.detail_loading),timeout=timeout)

    def detailroomsearch(self):  #随机选择房型与输入当前所选择的房间类型
        roomlen=len(self.find_elements(self.detail_roomlist))
        if roomlen>3:   #当前房间数大于3，则点击获取其它房型
            self.find_element(self.detail_morerometype).click()
        roomnumber=int(uniform(1,roomlen+1)) #生成随机整数
        roomstate=self.find_element(self.detail_roomcheckstatu,roomnumber)
        if "ok" not in roomstate.get_attribute("className"):   #若当前房间已被选择，则不进行选择操作
            self.find_element(self.detail_roomcheck,roomnumber).click()
        self.detail_roomname=self.find_element(self.detail_roomtypename,roomnumber).get_attribute("title")

    def detailupdate(self,updateflight=None,updatehotel=None):   #更换机票or更换酒店
        if updateflight==True:
            self.find_element(self.detail_uptateflight).click()
        elif updatehotel==True:
            self.find_element(self.detail_updatehotel).click()
        else:
            raise SystemError

    def detailclick(self):   #detail提交预订
        try:
            self.find_element(self.detail_nobutton)    #判断立即按钮是否变灰，变灰则报错
        except:
            self.find_element(self.detail_yesbutton).click()
        else:
            print(u"立即按钮变灰，报错")
            raise SystemError













